import { login, getMenus } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
// import Layout from '@/layout'
// import router from '@/router'
const state = {
  name: '欢迎旅行者的到来，希望你可以保底不歪，十连双金！！！',
  token: getToken(),
  menus: []
  // routerMenus: []
}
const mutations = {
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  logout(state) {
    state.token = null
    state.menus = []
    removeToken()
  },
  setMenus(state, payload) {
    state.menus = payload
  }
  // setRouterMenus(state) {
  //   state.routerMenus = state.menus.map(item => {
  //     const children = item.children.map(item2 => {
  //       return {
  //         path: item2.path,
  //         component: () => import('@/views/' + item2.path + '/index'),
  //         meta: { title: item2.authName, icon: 'table' }
  //       }
  //     })
  //     children.length === 1 ? children.push({ path: 'wahahahahaha' }) : ''
  //     console.log(1)
  //     return {
  //       path: '/' + item.path,
  //       component: Layout,
  //       redirect: '/' + item.path + '/' + item.children[0].path,
  //       meta: { title: item.authName, icon: 'el-icon-s-help' },
  //       children
  //     }
  //   })
  //   console.log(2)
  //   router.addRoutes(state.routerMenus)
  // }
}
const actions = {
  // 登录
  async login(context, payload) {
    const res = await login(payload)
    context.commit('setToken', res.token)
  },
  // 获取左侧列表权限
  async getMenus(context) {
    const res = await getMenus()
    // 保存数据
    context.commit('setMenus', res)
    // context.commit('setRouterMenus')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

